import {
  addRole,
  deleteRole,
  queryMenuJson,
  queryRole,
  saveMenu,
  updateMenu,
  updateRole
} from '@/services/systemManage/role'
import {queryAllMenu} from '@/services/systemManage/menuManage'
import {storePrompt} from '@/store/modules/commonStore'
import {filter} from 'lodash'

export default {
  namespaced: true,
  state: {
    title: '',
    loading: false,
    tableData: [],
    editData: null,
    modelVisible: false,
    pagination: {},
    queryForm: {},
    menuVisible: false,
    menuData: [],
    currentRoleId: null,
    menuJson: []
  },
  mutations: {
    setEditData(state, payload) {

      state.editData = filter(state.tableData, {objectId: payload})[0]
    }
  },
  actions: {
    async query({commit, state}, payload) {
      payload = {...payload, ...state.queryForm}
      await queryRole(payload).then((obj) => {
        let {data} = obj.data
        let {total, current} = data
        commit('setState', {
          name: 'pagination',
          value: {...state.pagination, total: Number(total), current: Number(current)}
        })
        commit('setState', {name: 'tableData', value: data.records})
      })
    },

    async queryAllMenu({commit}) {
      let info = []
      await queryAllMenu().then((obj) => {
        const {Child, Parent} = obj.data.data
        for (let parent of Parent) {
          if (parent.name == '开发者工具') {
            continue
          }
          let children = filter(Child, {parentId: parent.objectId}).map((value) => {
            return {
              key: value.objectId,
              title: value.name
            }
          })
          info.push({key: parent.objectId, title: parent.name, children})
        }
      })
      commit('setState', {name: 'menuData', value: [{key: 'all', title: '全选', children: info}]})
    },

    async queryMenuJson({commit, state}) {
      await queryMenuJson({objectId: state.currentRoleId}).then((obj) => {
        let {success, data} = obj.data
        if (success && data) {
          commit('setState', {name: 'menuJson', value: JSON.parse(data.menuJson)})
        }
      })
    },

    async add({commit, dispatch}, payload) {
      await addRole(payload).then((obj) => {
        let {success} = obj.data
        storePrompt(success)
      })
      commit('setState', {name: 'loading', value: false})
      dispatch('query')
    },

    async addMenu({commit, dispatch, state}, payload) {
      if (state.menuJson.length > 0) {
        await updateMenu({roleId: state.currentRoleId, ...payload}).then((obj) => {
          let {success} = obj.data
          storePrompt(success)
        })
      }else {
        await saveMenu({roleId: state.currentRoleId, ...payload}).then((obj) => {
          let {success} = obj.data
          storePrompt(success)
        })
      }
    },

    async update({commit, dispatch}, payload) {
      await updateRole(payload).then((obj) => {
        let {success} = obj.data
        storePrompt(success)
      })
      commit('setState', {name: 'loading', value: false})
      dispatch('query')
    },

    async delete({commit, dispatch}, payload) {
      await deleteRole(payload).then((obj) => {
        let {success} = obj.data
        storePrompt(success)
      })
      dispatch('query')
    },

  }
}